﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using BidFunLib.DAO;
using BidFunLib.Entity;
using NHibernate;

namespace BidFunLib.Services
{
    public class BidUserManager
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(BidUserManager));

        /// <summary>
        /// The dao of this manager
        /// </summary>
        private BidUserDAO dao = new BidUserDAO();


        /// <summary>
        /// Singleton instance
        /// </summary>
        private static BidUserManager instance;

        /// <summary>
        /// Retreive singleton of this class
        /// </summary>
        public static BidUserManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new BidUserManager();
                }
                return BidUserManager.instance;
            }
            set
            {
                BidUserManager.instance = value;
            }
        }

        private BidUserManager()
        {
            ListUser = new List<BidUser>(); 
        }

        /// <summary>
        /// 
        /// </summary>
        public List<BidUser> ListUser { get; set; }

        /// <summary>
        /// return the user corresponding to the pseudo
        /// search is perfomed in memory in the user list who have bet during this session
        /// </summary>
        /// <param name="pseudo"></param>
        /// <returns></returns>
        public BidUser getUserByPseudo(String pseudo)
        {
            // search in memory list
            foreach (BidUser user in ListUser)
            {
                if (user.Pseudo == pseudo)
                {
                    log.Debug("User " + user.Pseudo + " already exist in memory.");
                    return user;
                }
            }

            // not found so search in database or create if not exist
            BidUser newUser = retreiveOrCreateBidUserByPseudo(pseudo);
            ListUser.Add(newUser);
            return newUser;
        }
    

        /// <summary>
        /// Delegate to DAO the management of the user search or creation
        /// </summary>
        /// <param name="pseudo"></param>
        /// <returns></returns>
        private BidUser retreiveOrCreateBidUserByPseudo(String pseudo) {
            BidUser bidUser =  null;

                // first of all we search for pseudo in DB
                bidUser = dao.getBidUserByPseudo(pseudo);

                // not found so we save a new one
                if (bidUser == null) {
                    log.Debug("User " + pseudo + " must be created and saved into database.");
                    bidUser = new BidUser();
                    bidUser.Pseudo = pseudo;

                    dao.saveBidUser(bidUser);
                
                    bidUser = dao.getBidUserByPseudo(pseudo);
                }
            
            return bidUser;
        }


        /// <summary>
        /// Retourne la liste des users qui ont enchéris sur une vente
        /// </summary>
        /// <param name="idBidSale"></param>
        /// <returns></returns>
        /*public IList<BidUser> getAllUserForSpecificIdBidSale(Int32 idBidSale)
        {
            IList<BidUser> users = null;

            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                users = (IList<BidUser>)session.CreateQuery("select distinct bu from BidLogger bo, BidUser bu where bo.IdBidSale= ? AND bo.idBidUser = bu.IdBidUser")
                .SetInt32(0, idBidSale)
                .List<BidUser>();
                
                transaction.Commit();
                session.Close();
                return users;
            }
        }*/

        /// <summary>
        /// Méthode appelé à chque enchere réalisée par un user
        /// update statsBidUser & StatsBidUserByObject
        /// </summary>
        /// <param name="bidLogger"></param>


        public void updateGlobalUsersStatistics(BidLogger bidLogger)
        {
            StatsBidUserGlobal statsBidUserGlobal = new StatsBidUserGlobal();
        }



        public BidUser getUserById(Int32 id)
        {
           return dao.getUserById(id);
        }
    }
}
